package com.yj.easydialog.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import java.util.EnumMap;

/**
 * Created by Sigthp-P-Department on 2018/4/4.
 */

class AlertController {
    private AlertDialog mAlertDialog;
    private Window mWindow;
    private DialogViewHelper mDialogViewHelper;

    public AlertDialog getmAlertDialog() {
        return mAlertDialog;
    }

    public Window getmWindow() {
        return mWindow;
    }

    public AlertController(AlertDialog alertDialog, Window window) {
        this.mAlertDialog=alertDialog;
        this.mWindow=window;
    }
    public void setTextView(int viewId, CharSequence charSequence) {
        mDialogViewHelper.setTextView(viewId,charSequence);
    }

    public void setOnClickLisner(int viewId, View.OnClickListener onClickListener) {
        mDialogViewHelper.setOnClickLisner(viewId,onClickListener);
    }

    public <T extends View>T getView(int viewId) {
        return mDialogViewHelper.getView(viewId);
    }

    public void setDialogViewHelper(DialogViewHelper dialogViewHelper) {
        this.mDialogViewHelper = dialogViewHelper;
    }

    public static class AlertParams {
        public Context mContext;
        public int mThemeResId;
        public boolean mCancelable=true;
        public boolean mCanceledOnTouchOutside=true;
        public DialogInterface.OnCancelListener mOnCancelListener;
        public DialogInterface.OnDismissListener mOnDismissListener;
        public DialogInterface.OnKeyListener mOnKeyListener;
        public SparseArray<CharSequence> mTextViewArray=new SparseArray<>();
        public SparseArray<View.OnClickListener> mClickArray=new SparseArray<>();
        public View mView;
        public int mViewLayoutResId=0;
        public int mWidth= ViewGroup.LayoutParams.WRAP_CONTENT;
        public int mAnimationId=0;
        public int mGravity= Gravity.CENTER;
        public int mHeight= ViewGroup.LayoutParams.WRAP_CONTENT;

        public AlertParams(Context context, int themeResId) {
            mContext=context;
            mThemeResId=themeResId;
        }

        public void apply(AlertController mAlert) {
            DialogViewHelper dialogViewHelper = null;
            //设置布局
            if (mViewLayoutResId != 0) {
                dialogViewHelper = new DialogViewHelper(mContext, mViewLayoutResId);
            }

            if (mView != null) {
                dialogViewHelper = new DialogViewHelper();
                dialogViewHelper.setContentView(mView);
            }

            if (dialogViewHelper == null) {
                throw new IllegalArgumentException("请设置布局");
            }
            mAlert.setDialogViewHelper(dialogViewHelper);

            mAlert.getmAlertDialog().setContentView(dialogViewHelper.getContentView());
            //设置文本
            int mTextViewArraySize = mTextViewArray.size();
            for (int x = 0; x < mTextViewArraySize; x++) {
                mAlert.setTextView(mTextViewArray.keyAt(x), mTextViewArray.valueAt(x));
            }

            //设置点击
            int mClickArraySize = mClickArray.size();
            for (int x = 0; x < mClickArraySize; x++) {
                mAlert.setOnClickLisner(mClickArray.keyAt(x), mClickArray.valueAt(x));
            }


            //配置自定义效果，全屏，是否底部，动画等。
            Window window = mAlert.getmWindow();
            window.setGravity(mGravity);
            if (mAnimationId != 0) {
                window.setWindowAnimations(mAnimationId);
            }
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = mWidth;
            params.height = mHeight;
            window.setAttributes(params);

        }
    }
}
